<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <router-link to="/home" style="text-decoration: none;">
        <div class="logo_tile">
          <h2>用户管理系统</h2>
        </div>
      </router-link>
      <div class="logo_tile">
        <span >欢迎：{{loginUser}} &nbsp;&nbsp;&nbsp;</span>
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>

    <!-- 下面的主体区域 -->
    <el-container>
      <!-- 左侧的 menu 菜单 -->
      <el-aside :width="iscollapse ? '65px' : '200px'">
        <!-- 折叠展开menu的bar -->
        <div class="toggleBar" @click="iscollapse=!iscollapse">|||</div>
        <!-- menu 菜单 -->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          router
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          active-text-color="#409EFF">
          <el-submenu index="index" :class="{'el_submenu_large':!iscollapse,'el_submenu_small':iscollapse}" v-for="(item,index) in menuList ">
              <template slot="title">
                <i :class="item.icon"></i>
                <span slot="title">{{item.menuName}}</span>
              </template>
            <el-menu-item :index="menu.url" v-for="menu in item.children"><i class="menu.icon"></i>{{menu.menuName}}</el-menu-item>
              <!--<el-menu-item index="userList"><i class="el-icon-user-solid"></i>用户管理</el-menu-item>
              <el-menu-item index="roleList"><i class="el-icon-s-custom"></i>角色管理</el-menu-item>
              <el-menu-item index="menus"><i class="el-icon-menu"></i>菜单管理</el-menu-item>
              <el-menu-item index="1-4-4"><i class="el-icon-s-check"></i>权限管理</el-menu-item>-->
          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 右侧的 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
    <span style="color: gray; position: fixed; bottom: 0; right: 0;">用户管理系统</span>
  </el-container>
</template>

<script>
    export default {
      name: "Home",
      data() {
        return {
          iscollapse: false,
          loginUser:'',
          menuList:[]
        }
      },
      mounted(){
        let user = JSON.parse(sessionStorage.getItem("user"));
        this.loginUser=user.fullName

        let id = user.id
        this.getTreeByUserId(id);
      },
      methods: {
        // 点击按钮，退出登录
        logout() {
          // 清空保存的 token 信息
          window.sessionStorage.removeItem('user')
          // 强制跳转到登录页面
          this.$router.push('/login')
        },
        getTreeByUserId(id) {
          //请求后端
          this.$http.get("/menu/getTreeByUserId/"+id).then(
            (res) => {
              if (res.data.success) {
                this.menuList = res.data.result;
                console.log(res);
                this.$message.success(res.data.message);
              } else {
                this.$message.error(res.data.message)
              }

            }
          );
        }
      }
    }
</script>

<style scoped lang="less">
  .home-container {
    height: 100%;
  }
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0;
    user-select: none;
    .logo_tile {
      display: flex;
      align-items: center;
      color: white;
      h2 {
        font-weight: 200;
        margin-left: 15px;
      }
    }
    .el-button {
      margin-right: 10px;
    }
  }

  .el-aside {
    background-color: #333744;
    user-select: none;
  }

  .el-main {
    background-color: #eaedf1;
  }

  .iconfont {
    margin-right: 8px;
  }

  .toggleBar {
    color: #fff;
    font-size: 12px;
    line-height: 24px;
    background-color: #4a5064;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
    user-select: none;
  }

  .el_submenu_large {
    width: 200px;
  }
  .el_submenu_small {
    width: 65px;
  }
</style>
